package com.small;

import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.view.KeyEvent;
import android.widget.Toast;

import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.Common;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.db.DbTool;
import com.small.model.SQParentModel;
import com.smell.R;

public class Core extends Activity {

	private static boolean show_splash;
	private static final int STOPSPLASH = 0;
	private static final long SPLASHTIME = 1500;

	int result = -1;
	Action action;
	HashMap<String, Object> resultSQMap;
	List<SQParentModel> shopSQList;
	DbTool dbtool;
	private Handler splashHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOPSPLASH:
				initData();
				break;
			}
			super.handleMessage(msg);
		}
	};  

	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_splash2);
		NetWorkStatus();
		dbtool = new DbTool(Core.this);		
		show_splash = true;
		action = new Action(Core.this);
		super.onCreate(savedInstanceState);
	}
	private boolean NetWorkStatus() {
		boolean netSataus = false;
		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		cwjManager.getActiveNetworkInfo();
		if (cwjManager.getActiveNetworkInfo() != null) {
			netSataus = cwjManager.getActiveNetworkInfo().isAvailable();
		}
		if (!netSataus) {
			Builder b = new AlertDialog.Builder(this).setTitle("网络检查")
					.setMessage("请对网络进行设置");
			b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent mIntent = new Intent(
							Settings.ACTION_WIRELESS_SETTINGS);
					startActivityForResult(mIntent, 0); // 如果在设置完成后需要再次进行操作，可以重写操作代码，在这里不再重写
				}
			}).setOnKeyListener(l).show();
		} else {
			Toast.makeText(this, "网络状态正常", Toast.LENGTH_SHORT).show();
		}
		return netSataus;
	}

	OnKeyListener l = new OnKeyListener() {

		@Override
		public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
			if (arg2.getKeyCode() == 4) {
				return true;
			}
			return false;
		}
	};
	private void initData() {
		SharedPreferences sp = getSharedPreferences(Common.Userbase_Info, 0);
		Common.isAutoLogin = sp.getBoolean(Common.Userbase_isAutoLogin, false);
		Common.userName = sp.getString(Common.Userbase_UserName, "");
		Common.userPass = sp.getString(Common.Userbase_Pass, "");
		
		loadSQData();
		
	}

	private void loadSQData() { 
		new AsyncDataLoader(asyncSQCallback).execute();
		 
	}
	
	private Callback asyncSQCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() { 
			if (Common.isAutoLogin) {
				result = action.doLogin(Common.userName ,Common.userPass);				
			}			
			resultSQMap = action.loadSQ("1");
			resultSQMap = action.loadShopType("1");
		}

		@Override
		public void onPrepare() {
		} 
		 
		@Override
		public void onFinish() {  
			
			if (result == 1) {
				action.showShortToast("登录成功");
			} 
			
			Intent i = new Intent(Core.this, HomeActivity.class);
			Core.this.startActivity(i);
			Core.this.finish();
		}
	};	 
	 
	protected void onResume() {
		super.onResume();
		if (show_splash) { 
			Message msg = new Message();
			msg.what = STOPSPLASH;
			splashHandler.sendMessageDelayed(msg, SPLASHTIME);
			show_splash = false;
		}
	}
	@Override
	protected void onDestroy() {		 
		super.onDestroy();
		if ( Common.dbtool != null) {
			Common.dbtool.close();
			Common.dbtool = null;
		}
	}  
}